package com.luping.xiao.xiao.utils.Thread;

import com.luping.xiao.xiao.domain.SysUser;
import com.luping.xiao.xiao.service.SysUserService;
import org.apache.commons.lang3.concurrent.BasicThreadFactory;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;

/**
 * 消费者
 */
@Component("threadRunConsumer")
public class ThreadRunConsumer {
    protected static final Logger log = LoggerFactory.getLogger(ThreadRunConsumer.class);
    private static ScheduledExecutorService executorServiceConsumer = new ScheduledThreadPoolExecutor(100,
            new BasicThreadFactory.Builder().namingPattern("example1-schedule-pool-%d").daemon(true).build());
    @Autowired
    private SysUserService sysUserService;

    public void insertList() {
        LinkedBlockingQueue<List<SysUser>> queue = ThreadQueue.gatQueue();
        while (queue.size() > 0) {
            log.info("插入数据库线程调用");
            executorServiceConsumer.execute(new Runnable() {
                @Override
                public void run() {
                    log.info("正在添加。。。。"+queue.size());
                    sysUserService.insertList(queue.poll());
                }
            });
        }
        log.info("添加结束");
        ThreadQueue.currentTaskStat = ThreadQueue.EXTRACTOR_STAT.STORE_DONE;
    }


}
